<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod__news">
      <el-form :inline="true" :model="query" @keyup.enter.native="getData()">
    <el-form-item>
    <el-input v-model="query.title" placeholder="标题" clearable></el-input>
    </el-form-item>
    <el-form-item>
    </el-form-item>
           <el-form-item>
          <el-button @click="getData()">查询</el-button>
          <el-button @click="reset()">重置</el-button>
   </el-form-item>
   <el-form-item>
          <el-button type="primary" @click="addNewsInfo">添加</el-button>
        </el-form-item>
   <el-form-item>
          <el-button :disabled="this.multipleSelection.length === 0" type="danger" @click="delBatchNewsInfo">批量删除</el-button>
   </el-form-item>
  </el-form>
      <el-table v-loading="tableDataLoading" :data="tableData" border @selection-change="selectionChange" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
       <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
          <el-table-column prop="content" label="公告信息" header-align="center" align="center"></el-table-column>

          <el-table-column prop="imageUrl" label="图片"  header-align="center" align="center">
          <template slot-scope="scope">
              <el-image
                      style="width: 200px; height:200px"
                      :src=" scope.row.imageUrl"
                      :preview-src-list="[scope.row.imageUrl]">
              </el-image>
          </template>
       </el-table-column>
       <el-table-column prop="type" label="类型"  header-align="center" align="center">
         <template slot-scope="scope">
             <span v-if="scope.row.type === 1">公告</span>
             <span v-if="scope.row.type === 2">教学通知</span>
         </template>
       </el-table-column>
       <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="updateNewsInfo(scope.row)">更新</el-button>
            <el-button type="danger" size="small" @click="delNewsInfo(scope.row.id)">删除</el-button>
          </template>
      </el-table-column>
     </el-table>
      <el-pagination
              :current-page="query.pageNum"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="query.pageSize"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="pageSizeChange"
              @current-change="pageCurrentChange">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <el-dialog :visible.sync="addOrUpdateVisible" :title="!form.id ? '新增' : '编辑'" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form :model="form" :rules="rule" ref="form" @keyup.enter.native="formSubmitHandle()" label-width="100px">
            <el-form-item label="标题" prop="title">
                <el-input v-model="form.title" placeholder="标题"></el-input>
            </el-form-item>
    <el-form-item label="公告信息" prop="content">
      <el-input v-model="form.content" placeholder="公告信息"></el-input>
    </el-form-item>

      <el-form-item label="图片" prop="imageUrl">
          <single-upload v-model="form.imageUrl"></single-upload>
      </el-form-item>
 </el-form>
        <template slot="footer">
          <el-button @click="addOrUpdateVisible = false">取消</el-button>
          <el-button type="primary" @click="formSubmitHandle()">确认</el-button>
        </template>
      </el-dialog>

    </div>
  </el-card>
</template>

<script>
    import { } from '../../utils/validator'

    import {getNewsInfo,saveNewsInfo,updateNewsInfo,delNewsInfo,delBatchNewsInfo} from '../../api/news'
                            import SingleUpload from '../common/singleUpload';


  export default {
    components: {
    SingleUpload
    },

    data () {
      return {
        tableData: [],
        query: {
          pageNum: 1,
          pageSize: 10,
          title:null,
          userId:null,
    realName:null,
                },
        form: {},
        multipleSelection: [],
        tableDataLoading: false,
        total: 0,
        addOrUpdateVisible: false,
        rule: {
         content: [{required: true, message: '公告信息不得为空', trigger: "blur"}],
         title: [{required: true, message: '标题不得为空', trigger: "blur"}],
         createTime: [{required: true, message: '创建时间不得为空', trigger: "blur"}],
         userId: [{required: true, message: '用户不得为空', trigger: "blur"}],
         imageUrl: [{required: true, message: '图片不得为空', trigger: "blur"}],
         type: [{required: true, message: '类型不得为空', trigger: "change"}],
        },
          baseUrl:''
    }
   },
    created() {
      this.getData();
    },

    methods:{
      /**
       * 获取所有新闻公告
       */
      getData(){
          this.query.type = 1;
        getNewsInfo(this.query).then(res =>{
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.baseUrl = res.baseUrl;
        })

      },

      /**
       * 打开添加框
       */
      addNewsInfo(){
        this.form = {};
        this.form.id = null;
        if (this.$refs['form'] !==undefined){
          this.$refs['form'].resetFields();
        }
        this.addOrUpdateVisible = true;
      },

      /**
       * 打开更新框
       */
      updateNewsInfo(row){
        this.form = JSON.parse(JSON.stringify(row));
        this.addOrUpdateVisible = true;
        },

      /**
       * 按住enter键或者确定按钮提交数据,对后台发请求
       * 如果id为null,就是走添加新闻公告的接口
       * 如果id不为null,就是走更新新闻公告的接口
       */
      formSubmitHandle(){
        this.$refs['form'].validate(valid => {
                if(valid){
          if (this.form.id === null){
              this.form.type = 1;
            saveNewsInfo(this.form).then(res =>{
              this.$message.success('添加成功');
              this.getData();
              this.addOrUpdateVisible = false;
            })
          }else {
            updateNewsInfo(this.form).then(res =>{
              this.$message.success('更新成功');
              this.getData();
              this.addOrUpdateVisible = false;
            })
          }
          // this.$refs['form'].resetFields();


        }
        });
      },

      /**
       *单个删除数据
       */
      delNewsInfo(id){
        this.$confirm('确定要删除所选择的吗？', '提示', {
          type: 'warning'
        }).then(action => {
          if (action === 'confirm') {
            delNewsInfo(id).then(res=>{
              if (res.code === 1){
                this.$message.success('删除成功');
                this.getData();
              }else {
                return this.$message.error(res.msg)
              }

            })
          }
        }).catch(() => {});

      },



      /**
       * 选中table表格事件
       */
      selectionChange(val){
        this.multipleSelection = [];
        val.forEach((item) => {
          this.multipleSelection.push(item.id);
        });
      },

        /**
         * 批量删除新闻公告的接口
         */
      delBatchNewsInfo(){
        delBatchNewsInfo(this.multipleSelection.join(',')).then(res =>{
          this.$message.success('批量删除成功');
          this.getData();
        })
      },

      /**
       * 重置数据
       */
      reset(){
        this.query.title = null;
        this.query.userId = null;
                                          this.query.realName=null
                                this.$set(this.query, 'pageNum', 1);
        this.getData();
      },


      /**
       * 分页(改变页码)
       * @param val
       */
      pageCurrentChange(val){
        this.$set(this.query, 'pageNum', val);
        this.getData();
      },

      /**
       * 分页(改变每页条数)
       * @param val
       */
      pageSizeChange(val){
        this.$set(this.query, 'pageSize', val);
        this.getData();
      },

      /**
       * 关闭弹窗
       */
      clearAddForm(){
        this.addOrUpdateVisible = false;
      }
    },


  }
</script>
